<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>linux安装mongo主从模式 | 清峰小栈</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="mongodb" />
  
  
  
  
  <meta name="description" content="Linux-Mongo集群模式之主从+副本集一、知识沉淀：Mongodb的replication主要有两种：主从和副本集（replica set）。现在mongodb官方建议用副本集替代主从复制。 什么是oplog： 　　MongoDB 的Replication是通过一个日志来存储写操作的，这个日志就叫做oplog。 在默认情况下,oplog分配的是5%的空闲磁盘空间。通常而言,这是一种合理的设置">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux安装mongo主从模式">
<meta property="og:url" content="http://example.com/2021/09/14/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/Linux%E5%AE%89%E8%A3%85mongo%E4%B8%BB%E4%BB%8E%E6%A8%A1%E5%BC%8F/index.html">
<meta property="og:site_name" content="清峰小栈">
<meta property="og:description" content="Linux-Mongo集群模式之主从+副本集一、知识沉淀：Mongodb的replication主要有两种：主从和副本集（replica set）。现在mongodb官方建议用副本集替代主从复制。 什么是oplog： 　　MongoDB 的Replication是通过一个日志来存储写操作的，这个日志就叫做oplog。 在默认情况下,oplog分配的是5%的空闲磁盘空间。通常而言,这是一种合理的设置">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/lindaifeng/my-images/raw/master/img/7236980-ec85513460bb5cd6.jpg">
<meta property="og:image" content="https://gitee.com/lindaifeng/my-images/raw/master/img/replication.png">
<meta property="og:image" content="https://gitee.com/lindaifeng/my-images/raw/master/img/image-20210911174818855.png">
<meta property="article:published_time" content="2021-09-14T14:24:06.000Z">
<meta property="article:modified_time" content="2023-05-03T14:04:53.397Z">
<meta property="article:author" content="清峰">
<meta property="article:tag" content="mongodb">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lindaifeng/my-images/raw/master/img/7236980-ec85513460bb5cd6.jpg">
  

  

  <link rel="icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  <link rel="apple-touch-icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <!-- <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> -->
  <link href="/qingfeng-blog/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("/qingfeng-blog/css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("/qingfeng-blog/css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("/qingfeng-blog/css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  
<link rel="stylesheet" href="/qingfeng-blog/css/style.css">


  
<script src="/qingfeng-blog/js/jquery-3.1.1.min.js"></script>

  
<script src="/qingfeng-blog/js/bootstrap.js"></script>


  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/qingfeng-blog/css/bootstrap.css" >

  
    
<link rel="stylesheet" href="/qingfeng-blog/css/dialog.css">

  

  

  
    <link rel="stylesheet" href="/qingfeng-blog/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/qingfeng-blog/css/vdonate.css" >
  

<meta name="generator" content="Hexo 6.3.0"></head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="155px" height="90px" alt="Hike News" src="/qingfeng-blog/css/images/mylogo-2.png">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/tags">标签</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="Type something..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/qingfeng-blog/',
        CONTENT_URL: '/qingfeng-blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/qingfeng-blog/js/insight.js"></script>


</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-运维安装/Linux安装mongo主从模式" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
<div id="container">
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Linux安装mongo主从模式
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/qingfeng-blog/2021/09/14/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/Linux%E5%AE%89%E8%A3%85mongo%E4%B8%BB%E4%BB%8E%E6%A8%A1%E5%BC%8F/" class="article-date">
	  <time datetime="2021-09-14T14:24:06.000Z" itemprop="datePublished">2021-09-14</time>
	</a>

      
    <a class="article-category-link" href="/qingfeng-blog/categories/Linux/">Linux</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		PV:<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Linux-Mongo集群模式之主从-副本集"><a href="#Linux-Mongo集群模式之主从-副本集" class="headerlink" title="Linux-Mongo集群模式之主从+副本集"></a>Linux-Mongo集群模式之主从+副本集</h1><h2 id="一、知识沉淀："><a href="#一、知识沉淀：" class="headerlink" title="一、知识沉淀："></a>一、知识沉淀：</h2><p><strong>Mongodb的replication主要有两种：主从和副本集（replica set）。现在mongodb官方建议用副本集替代主从复制。</strong></p>
<p><strong>什么是oplog：</strong></p>
<p>　　MongoDB 的Replication是通过一个日志来存储写操作的，这个日志就叫做oplog。 在默认情况下,oplog分配的是5%的空闲磁盘空间。通常而言,这是一种合理的设置。可以通过mongod –oplogSize来改变oplog的日志大小。</p>
<h4 id="主从复制："><a href="#主从复制：" class="headerlink" title="主从复制："></a>主从复制：</h4><p>主从的原理和mysql类似，主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。<br> 主服务器数据库的每次操作都会记录在其二进制文件mysql-bin.xxx（该文件可以在mysql目录下的data目录中看到）中，从服务器的I&#x2F;O线程使用专用账号登录到主服务器中读取该二进制文件，并将文件内容写入到自己本地的中继日志relay-log文件中，然后从服务器的SQL线程会根据中继日志中的内容执行SQL语句</p>
<p><img src="https://gitee.com/lindaifeng/my-images/raw/master/img/7236980-ec85513460bb5cd6.jpg" alt="img"></p>
<p>MySQL主从同步的作用</p>
<p>1、可以作为备份机制，相当于热备份<br>2、可以用来做读写分离，均衡数据库负载</p>
<h4 id="副本集："><a href="#副本集：" class="headerlink" title="副本集："></a><strong>副本集：</strong></h4><p><img src="https://gitee.com/lindaifeng/my-images/raw/master/img/replication.png" alt="MongoDB复制结构图"></p>
<h2 id="二、实战操作："><a href="#二、实战操作：" class="headerlink" title="二、实战操作："></a>二、实战操作：</h2><h3 id="1、Mongo主从模式"><a href="#1、Mongo主从模式" class="headerlink" title="1、Mongo主从模式"></a>1、Mongo主从模式</h3><p>主从模式是MongoDB最早的部署架构。从节点备份数据，在主节点挂了后，可以让从节点接替主节点；也可以做到读写分离，减轻主库压力。</p>
<p>主节点：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongod --dbpath=E:\mongodb\3.2.9\mongodb\db --logpath=E:\mongodb\3.2.9\mongodb\log\log.txt --logappend --port=27017 --master</span><br></pre></td></tr></table></figure>

<p>从节点</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">mongod --dbpath=E:\mongodb\3.2.9\slaveA\db --logpath=E:\mongodb\3.2.9\slaveA\log\log_slaveA.txt --logappend --port=27018 --slave --source=127.0.0.1:27017</span><br><span class="line"></span><br><span class="line">或者，再主节点启动后，手动添加从节点</span><br><span class="line">use local; -- local库的sources集合</span><br><span class="line">db.sources.insert(&#123;&quot;host&quot;:&quot;127.0.0.1:27017&quot;&#125;); -- 在子节点，指定主节点</span><br></pre></td></tr></table></figure>

<p>但是，主从有它很大的局限性，比如:主节点挂了不能自动切换连接，需要手动切换，这时候怎么办呢？</p>
<h3 id="2、Mongo副本集"><a href="#2、Mongo副本集" class="headerlink" title="2、Mongo副本集"></a>2、Mongo副本集</h3><p>复制集：由多个数据节点和选举节点组成，相连的数据节点只有一个是主节点，主节点负责接收写的操作，从节点是通过复制主节点来实现数据的同步。简单来说就是：从节点是通过数据的冗余来提高数据的可靠性。</p>
<p>从节点通过主节点的oplog文件，异步复制主节点的数据。</p>
<p>复制集基本角色：</p>
<p>1、标准节点：参与主节点primary的选举，自身宕机让出primary，复制读写操作。</p>
<p>2、从节点：从节点second不参与选举，只能被设置为被动节点。</p>
<p>3、仲裁节点：只参与投票，不负责存放数据。确保标准节点投票数不相同。</p>
<p><strong>mongo无认证的情况：</strong></p>
<p>配置文件如下【配置路径时需要确保  路径存在】</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">logpath=/var/lib/mongo/mongodb.log</span><br><span class="line"></span><br><span class="line">logappend=true</span><br><span class="line"></span><br><span class="line">dbpath=/var/lib/mongo/</span><br><span class="line"></span><br><span class="line">port = 27017</span><br><span class="line"></span><br><span class="line">journal=true</span><br><span class="line"></span><br><span class="line">quiet=true</span><br><span class="line"></span><br><span class="line">fork=true</span><br><span class="line"></span><br><span class="line">bind_ip=0.0.0.0</span><br></pre></td></tr></table></figure>

<p># 集群配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">replSet=replSet</span><br></pre></td></tr></table></figure>

<p>确保两边的防火墙关闭成功</p>
<p>复制集配置</p>
<p>将配置文件都修修改完之后</p>
<p>其中 replSet  是副本集名称，和conf文件中要保持一致</p>
<p>在mongod 控制台内输入</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rs.conf();</span><br></pre></td></tr></table></figure>

<p>提示找不到相关配置</p>
<p>##第一步：设置副本集内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">config = &#123;</span><br><span class="line"></span><br><span class="line">_id: &#x27;replSet&#x27;, </span><br><span class="line"></span><br><span class="line">members: [</span><br><span class="line"></span><br><span class="line">&#123;_id: 0, host: &#x27;192.168.179.3:27017&#x27;,priority:1&#125;,</span><br><span class="line"></span><br><span class="line">&#123;_id: 1, host: &#x27;192.168.179.4:27017&#x27;,priority:2&#125;,</span><br><span class="line"></span><br><span class="line">&#123;_id: 2, host: &#x27;192.168.179.5:27017&#x27;,priority:3&#125;</span><br><span class="line"></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>##第一步：初始化副本集</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rs.initiate(config) </span><br></pre></td></tr></table></figure>

<p>##第三步：查看副本集状态，找到private节点的IP </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rs.status()</span><br></pre></td></tr></table></figure>

<p>各个节点需要执行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rs.secondaryOk()</span><br></pre></td></tr></table></figure>

<p>主节点启动监听</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.enableFreeMonitoring()</span><br></pre></td></tr></table></figure>

<p><strong>mongo有认证的情况</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">1、yml配置文件启动 </span><br><span class="line">processManagement:</span><br><span class="line">   fork: true</span><br><span class="line">net:</span><br><span class="line">   bindIp: 0.0.0.0</span><br><span class="line">   port: 27017</span><br><span class="line">storage:</span><br><span class="line">   dbPath: /usr/local/mongodb/data/db</span><br><span class="line">systemLog:</span><br><span class="line">   destination: file</span><br><span class="line">   path: /usr/local/mongodb/logs/mongodb.log</span><br><span class="line">   logAppend: true</span><br><span class="line">storage:</span><br><span class="line">   journal:</span><br><span class="line">      enabled: true</span><br><span class="line"># 改动</span><br><span class="line">security:</span><br><span class="line">  # 开启认证</span><br><span class="line">  authorization: enabled</span><br><span class="line">  # 认证文件的路径</span><br><span class="line">  keyFile: &quot;/usr/local/mongodb/mongodb-keyfile&quot;</span><br><span class="line">  clusterAuthMode: keyFile</span><br><span class="line"></span><br><span class="line"># 改动</span><br><span class="line">replication:</span><br><span class="line">  oplogSizeMB: 10240</span><br><span class="line">  # 副本集名称</span><br><span class="line">  replSetName: replSet</span><br></pre></td></tr></table></figure>

<p>配置文件启动：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/mongod --config mongodb.yml</span><br></pre></td></tr></table></figure>

<p>生成 keyFile(认证文件)及配置-上传到到每个服务器上</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">openssl rand -base64 <span class="number">756</span>&gt; <span class="regexp">/root/</span>keyfile</span><br></pre></td></tr></table></figure>

<p>修改读写权限为 600    mongodb-keyfile</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chmod <span class="number">600</span> /mongo/mongodb-keyfile</span><br></pre></td></tr></table></figure>

<p>进入mongo输入：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">rs.conf();</span><br><span class="line">查看配置信息，无任何配置信息</span><br><span class="line">2021-09-11T17:32:30.840+0800 E QUERY    [thread1] Error: Could not retrieve replica set config: &#123;</span><br><span class="line">	&quot;info&quot; : &quot;run rs.initiate(...) if not yet done for the set&quot;,</span><br><span class="line">	&quot;ok&quot; : 0,</span><br><span class="line">	&quot;errmsg&quot; : &quot;no replset config has been received&quot;,</span><br><span class="line">	&quot;code&quot; : 94,</span><br><span class="line">	&quot;codeName&quot; : &quot;NotYetInitialized&quot;</span><br><span class="line">&#125; :</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>第一步：设置副本集内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">config = &#123;</span><br><span class="line">_id: &#x27;replSet&#x27;, </span><br><span class="line">members: [</span><br><span class="line">&#123;_id: 0, host: &#x27;主机ip:端口&#x27;,priority:1&#125;, //权重</span><br><span class="line">&#123;_id: 1, host: &#x27;主机ip:27017&#x27;,priority:2&#125;, //权重（salve）</span><br><span class="line">&#123;_id: 2, host: &#x27;主机ip:27017&#x27;,priority:3&#125; //权重高（master）</span><br><span class="line">]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">config = &#123;</span><br><span class="line">_id: &#x27;replSet&#x27;, </span><br><span class="line">members: [</span><br><span class="line">&#123;_id: 0, host: &#x27;192.168.179.3:27017&#x27;,priority:1&#125;,</span><br><span class="line">&#123;_id: 1, host: &#x27;192.168.179.4:27017&#x27;,priority:2&#125;,</span><br><span class="line">&#123;_id: 2, host: &#x27;192.168.179.5:27017&#x27;,priority:3&#125;</span><br><span class="line">]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>##第一步：初始化副本集</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">rs.initiate(config) </span><br><span class="line"></span><br><span class="line">返回&#123; &quot;ok&quot; : 1 &#125;成功</span><br><span class="line"></span><br><span class="line">返回错误信息</span><br><span class="line">&#123;</span><br><span class="line">	&quot;operationTime&quot; : Timestamp(1631352818, 2),</span><br><span class="line">	&quot;ok&quot; : 0,</span><br><span class="line">	&quot;errmsg&quot; : &quot;there are no users authenticated&quot;,</span><br><span class="line">	&quot;code&quot; : 13,</span><br><span class="line">	&quot;codeName&quot; : &quot;Unauthorized&quot;,</span><br><span class="line">	&quot;$clusterTime&quot; : &#123;</span><br><span class="line">		&quot;clusterTime&quot; : Timestamp(1631352818, 2),</span><br><span class="line">		&quot;signature&quot; : &#123;</span><br><span class="line">			&quot;hash&quot; : BinData(0,&quot;7qIwiTKM1e6QGQ5FpA23DSjUYTA=&quot;),</span><br><span class="line">			&quot;keyId&quot; : NumberLong(&quot;7006606872698421250&quot;)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">则需要认证</span><br><span class="line">use admin;</span><br><span class="line">db.auth(&#x27;root&#x27;,&#x27;123456&#x27;);</span><br><span class="line">返回1认证成功</span><br><span class="line"></span><br><span class="line">在执行配置命令</span><br><span class="line">rs.initiate(config) </span><br></pre></td></tr></table></figure>



<p>##第三步：查看副本集状态，找到private节点的IP </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rs.status()</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/lindaifeng/my-images/raw/master/img/image-20210911174818855.png" alt="image-20210911174818855"></p>
<p>这样配相当于主从，权重高的为主节点，权重低的为从节点。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//查看副本集主节点</span><br><span class="line">db.isMaster();</span><br><span class="line">//查看节点状态</span><br><span class="line">rs.status()</span><br><span class="line">//添加一个服务器为仲裁节点</span><br><span class="line">rs.addArb(ip:端口);</span><br><span class="line"></span><br><span class="line">//移除所有节点</span><br><span class="line">db.shutdownServer()</span><br><span class="line">//移除单个节点</span><br><span class="line">rs.remove(&#x27;192.168.56.105:27017&#x27;)</span><br></pre></td></tr></table></figure>

<p>主节点能操作，从节点同步数据，但从节点不能操作。</p>
<p>至此，mongo的集群搭建完毕。</p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>


<script src="/qingfeng-blog/js/vdonate.js"></script>

<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150832.png',
  alipayImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150823.png'
});
</script>
      
      
      <div>
        <ul class="post-copyright">
          <li class="post-copyright-author">
          <strong>Post author:  </strong>清峰</a>
          </li>
          <li class="post-copyright-link">
          <strong>Post link:  </strong>
          <a href="/qingfeng-blog/2021/09/14/运维安装/Linux安装mongo主从模式/" target="_blank" title="Linux安装mongo主从模式">http://example.com/2021/09/14/运维安装/Linux安装mongo主从模式/</a>
          </li>
          <li class="post-copyright-license">
            <strong>Copyright Notice:   </strong>
            All articles in this blog are licensed under <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
            unless stating additionally.
          </li>
         
        </ul>
<div>

      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/qingfeng-blog/tags/mongodb/" rel="tag">mongodb</a></li></ul>

      

    </footer>
  </div>
</div>
  
    
<nav id="article-nav">
  
    <a href="/qingfeng-blog/2021/11/30/Linux/Java%20%E5%BC%80%E5%8F%91%E5%B8%B8%E7%94%A8%E7%9A%84%20Linux%20%E5%91%BD%E4%BB%A4/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Java 开发常用的 Linux 命令
        
      </div>
    </a>
  
  
    <a href="/qingfeng-blog/2021/09/11/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/MongoDB%E7%9A%84%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%BC%80%E5%90%AF%E5%AE%A1%E8%AE%A1%E6%97%A5%E5%BF%97/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">MongoDB的安装配置与开启审计日志</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Linux-Mongo%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F%E4%B9%8B%E4%B8%BB%E4%BB%8E-%E5%89%AF%E6%9C%AC%E9%9B%86"><span class="nav-number">1.</span> <span class="nav-text">Linux-Mongo集群模式之主从+副本集</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E7%9F%A5%E8%AF%86%E6%B2%89%E6%B7%80%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">一、知识沉淀：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%EF%BC%9A"><span class="nav-number">1.1.0.1.</span> <span class="nav-text">主从复制：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%89%AF%E6%9C%AC%E9%9B%86%EF%BC%9A"><span class="nav-number">1.1.0.2.</span> <span class="nav-text">副本集：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AE%9E%E6%88%98%E6%93%8D%E4%BD%9C%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">二、实战操作：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1%E3%80%81Mongo%E4%B8%BB%E4%BB%8E%E6%A8%A1%E5%BC%8F"><span class="nav-number">1.2.1.</span> <span class="nav-text">1、Mongo主从模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2%E3%80%81Mongo%E5%89%AF%E6%9C%AC%E9%9B%86"><span class="nav-number">1.2.2.</span> <span class="nav-text">2、Mongo副本集</span></a></li></ol></li></ol></li></ol>
    
    </div>
  </aside>


</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <!-- <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p> -->
	      <p>May there be no bug in the world！</p>
        <p id="copyRightEn">Copyright &copy; 2022 - 2023 All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				UV : <span id="busuanzi_value_site_uv"></span> |  
				PV : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/qingfeng-blog/" class="mobile-nav-link">首页</a>
  
    <a href="/qingfeng-blog/categories" class="mobile-nav-link">分类</a>
  
    <a href="/qingfeng-blog/tags" class="mobile-nav-link">标签</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  
<link rel="stylesheet" href="/qingfeng-blog/fancybox/jquery.fancybox.css">

  
<script src="/qingfeng-blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/qingfeng-blog/js/scripts.js"></script>



  
<script src="/qingfeng-blog/js/home.js"></script>




  
<script src="/qingfeng-blog/js/dialog.js"></script>









	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            清峰小栈
          </div>
          <div class="panel-body">
            Copyright © 2023 清峰 All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/qingfeng-blog/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>